﻿using Max.PH5_2103A.Shopping.TEntityFrame.DTO;
using Max.PH5_2103A.Shopping.TEntityFrame.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2103A.Shopping.TEntityFrame.API.Controllers
{
    /// <summary>
    /// 基本的泛型控制器（实现单表的增删改查）
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Route("base")]
    [ApiController]
    public class BaseController<T> : ControllerBase where T : class
    {

        IBaseService<T> _service;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        public BaseController(IBaseService<T> service)
        {
            _service = service;
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public virtual IActionResult Get()
        {
            ResponseResultDto response = new();

            var data = _service.Select();
            response.Data = data;

            return Ok(response);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public virtual IActionResult Insert(T t)
        {
            ResponseResultDto response = new();

            var data = _service.Insert(t);
            response.Data = data;

            return Ok(response);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public virtual IActionResult Update(T t)
        {
            ResponseResultDto response = new();

            var data = _service.Update(t);
            response.Data = data;

            return Ok(response);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public virtual IActionResult Delete(List<T> t)
        {
            ResponseResultDto response = new();

            var data = _service.Delete(t);
            response.Data = data;

            return Ok(response);
        }
    }
}
